package com.xinhua.erp.service;

import com.xinhua.erp.pojo.PageResult;
import com.xinhua.erp.pojo.Result;
import com.xinhua.erp.pojo.Retail;
import com.xinhua.erp.pojo.Supplier;
import com.xinhua.erp.repo.RetailRepo;
import com.xinhua.erp.repo.SupplierRepo;
import com.xinhua.erp.utils.IdWorker;
import com.xinhua.erp.utils.OrderNumber;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Map;

@Service
@Transactional
public class RetailService {
    @Autowired
    private RetailRepo retailRepo;

    @Autowired
    private SupplierRepo supplierRepo;

    @Autowired
    private IdWorker idWorker;
    public Result updateStatus(Long id){

        retailRepo.updateStatus(id);

        return new Result(true,20000,"审核成功");

    }

    public Result findById(long id){

        return Result.OK(retailRepo.getOne(id));
    }
    public Result findAll(String type,int page,int size){

        Pageable pageable = PageRequest.of(page - 1, size);

        Page<Retail> retails = retailRepo.findAllByType(type,pageable);

        return Result.OK(new PageResult<>(retails.getTotalElements(),retails.getContent()));
    }
    public Result deleteById(long id){

        retailRepo.deleteById(id);

        return Result.OK();
    }

    public Result save(Retail retail){

        retail.setId(idWorker.nextId());

        //设置默认值

        retail.setOrderNumber(OrderNumber.getXs());
        if (retail.getType().equals("销售订单")){
            retail.setStatus("未审核");

        }
        retailRepo.save(retail);

        if (retail.getType().equals("销售出库")){
            Supplier one = supplierRepo.getOne(retail.getOrganId());
            one.setAllneedget(one.getAllneedget().add(retail.getAmount()));
            supplierRepo.save(one);
        }

        return Result.OK();
    }

    public Result update(Long id,Retail retail){
        retail.setId(id);
        retailRepo.save(retail);
        return Result.OK();
    }
    public Result Search(Map searchMap, int page, int size) {

        Pageable pageable = PageRequest.of(page - 1, size);

        Page<Retail>retails = retailRepo.findAll(createSpecification(searchMap), pageable);

        return Result.OK(new PageResult<>(retails.getTotalElements(), retails.getContent()));
    }

    private Specification<Retail> createSpecification(Map searchMap){
        return new Specification<Retail>() {
            @Override
            public Predicate toPredicate(Root<Retail> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Predicate> predicateList = new ArrayList<>();
                if(!StringUtils.isBlank((String)searchMap.get("orderNumber"))){
                    predicateList.add(criteriaBuilder.like(root.get("orderNumber").as(String.class),"%"+searchMap.get("orderNumber")+"%"));
                }
                if(!StringUtils.isBlank((String)searchMap.get("productName"))){
                    predicateList.add(criteriaBuilder.like(root.get("productName").as(String.class),"%"+searchMap.get("productName")+"%"));
                }
                if(!StringUtils.isBlank((String)searchMap.get("time"))){
                    predicateList.add(criteriaBuilder.like(root.get("time").as(String.class),"%"+searchMap.get("time")+"%"));
                }

                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}


